package event

import (
	"context"
	"ez/apps/tool/service"
	"ez/apps/wms/auto/mc"
	"ez/apps/wms/conf"
	wms "ez/apps/wms/document"
	"ez/apps/wms/services"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/tools"
)

func init() {
	ez.Subscribe(mc.OutLogEventAfterSave, func(v interface{}, ctx context.Context) {
		md, ok := v.(*wms.OutLog)
		if ok {
			md.LoadType()
			//如果不填单号就自动创建
			if md.Sn == "" {
				//创建批次号
				sn := service.CreateSnByString(ez.ConfigService.AppId, "StoreOutSn:[YY][MM][DD][DMAX[3]]", 1)
				md.Sn = sn[len("StoreOutSn:"):]
			}
			//如果不填批次号就自动创建
			if md.BatchCode == "" {
				//创建批次号
				sn := service.CreateSnByString(ez.ConfigService.AppId, "StoreOutBatch:[YY][MM][DD][DMAX[3]]", 1)
				md.BatchCode = sn[len("StoreOutBatch:"):]
			}
			//如果不填堆号就自动创建
			if md.HeapCode == "" {
				//创建批次号
				sn := service.CreateSnByString(ez.ConfigService.AppId, "StoreOutHeap:[YY][MM][DD][DMAX[3]]", 1)
				md.HeapCode = sn[len("StoreOutHeap:"):]
			}

			if md.Link == "" {
				md.Link = tools.CreateRandString(64)
			}
			md.SetEvent(false)
			_ = md.Save()
			services.CreateChangeLog(md.ItemId, conf.ChangeLogOut)
			//注：这里不需要个更新库存信息因为，出库一定会修改入库记录，入库记录会更新库存信息
		}
	})

}
